//
//  MineViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/2.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "MineViewController.h"
#import "SettingViewController.h"
#import "MineCollectionViewCell.h"
#import "MineClassListViewController.h"
#import "MineHistoryViewController.h"
#import "MineFocusViewController.h"
#import "UserInfoViewController.h"
#import "LoginViewController.h"
#import <UIImageView+WebCache.h>
#import "MineHelpViewController.h"

@interface MineViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSArray *dataArr;
@property (strong, nonatomic) UIImageView *faceImageview;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *descriptionLabel;
@end

@implementation MineViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserInfo) name:@"klogin_notification" object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArr = @[@{@"title":@"浏览历史", @"image":@"mine_history"}, @{@"title":@"我的课程", @"image":@"mine_class"}, @{@"title":@"我的关注", @"image":@"mine_focus"}, @{@"title":@"帮助", @"image":@"mine_help"}, @{@"title":@"设置", @"image":@"mine_setting"}];
    [self setupView];
    [self setupNavigation];
}

- (void)setupNavigation {
    
    self.tabBarController.navigationItem.leftBarButtonItem = nil;
    self.tabBarController.navigationItem.rightBarButtonItem = nil;
    self.tabBarController.navigationItem.titleView = nil;
    self.tabBarController.navigationItem.title = @"我的";
}

- (void)setupView {
    
    UIView *topview = [[UIView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, 106)];
    topview.backgroundColor = COLOR(245, 245, 245, 1);
    [self.view addSubview:topview];
    
    UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 96)];
    backview.backgroundColor = [UIColor whiteColor];
    [topview addSubview:backview];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toUserInfo)];
    [backview addGestureRecognizer:tap];
    
    self.faceImageview = [[UIImageView alloc] initWithFrame:CGRectMake(16.0, 18, 60, 60)];
    _faceImageview.layer.cornerRadius = 30.0;
    _faceImageview.layer.masksToBounds = YES;
    if ([UserModel shared].hasLogin) {
        [_faceImageview sd_setImageWithURL:[NSURL URLWithString:[UserModel shared].loginUser.avatar]];
    }else {
        _faceImageview.image = [UIImage imageNamed:@"user_icon"];
    }
    [backview addSubview:_faceImageview];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 26, ScreenWidth - 126, 21)];
    _nameLabel.font = [UIFont boldSystemFontOfSize:16.0];
    _nameLabel.textColor = COLOR(65, 65, 65, 1);
    [backview addSubview:_nameLabel];
    
    self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 53, ScreenWidth - 126, 21)];
    _descriptionLabel.font = [UIFont systemFontOfSize:13.0];
    _descriptionLabel.textColor = COLOR(215, 215, 215, 1);
    [backview addSubview:_descriptionLabel];
    
    if ([UserModel shared].hasLogin) {
        _nameLabel.text = [UserModel shared].loginUser.nameString;
        _descriptionLabel.text = @"查看并编辑个人资料";
    }else {
        _nameLabel.text = @"点击登录/注册";
        _descriptionLabel.text = @"查看并编辑个人资料";
    }
    
    UIImageView *right = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, 35, 30, 30)];
    right.image = [UIImage imageNamed:@"arrow_right"];
    [backview addSubview:right];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 25;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(33, 0, 25, 0);
    layout.itemSize = CGSizeMake(ScreenWidth / 3.0, 67);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight + 106, ScreenWidth, ScreenHeight - XNavigationHeight - 106) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerNib:[UINib nibWithNibName:@"MineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MineCollectionViewCell"];
    [self.view addSubview:_collection];
}

- (void)refreshUserInfo {
    
    if ([UserModel shared].hasLogin) {
        [_faceImageview sd_setImageWithURL:[NSURL URLWithString:[UserModel shared].loginUser.avatar]];
        _nameLabel.text = [UserModel shared].loginUser.nameString;
        _descriptionLabel.text = @"查看并编辑个人资料";
    }else {
        _faceImageview.image = [UIImage imageNamed:@"user_icon"];
        _nameLabel.text = @"点击登录/注册";
        _descriptionLabel.text = @"查看并编辑个人资料";
    }
}

- (void)toUserInfo {
    
    if ([UserModel shared].hasLogin) {
    
        UserInfoViewController *controller = [[UserInfoViewController alloc] init];
        [self.tabBarController.navigationController pushViewController:controller animated:YES];
    }else {

        UINavigationController *controller = [LoginViewController loginViewController];
        [self.tabBarController.navigationController presentViewController:controller animated:YES completion:nil];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineCollectionViewCell" forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(MineCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    cell.titleLabel.text = _dataArr[indexPath.item][@"title"];
    cell.imageView.image = [UIImage imageNamed:_dataArr[indexPath.item][@"image"]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 4) {
        if ([UserModel shared].hasLogin) {
            
            SettingViewController *controller = [[SettingViewController alloc] init];
            [self.tabBarController.navigationController pushViewController:controller animated:YES];
        }else {
            
            UINavigationController *controller = [LoginViewController loginViewController];
            [self.tabBarController.navigationController presentViewController:controller animated:YES completion:nil];
        }
    }else if (indexPath.item == 0) {
        if ([UserModel shared].hasLogin) {
            
            MineHistoryViewController *controller = [[MineHistoryViewController alloc] init];
            [self.tabBarController.navigationController pushViewController:controller animated:YES];
        }else {
            
            UINavigationController *controller = [LoginViewController loginViewController];
            [self.tabBarController.navigationController presentViewController:controller animated:YES completion:nil];
        }
    }else if (indexPath.item == 1) {
        if ([UserModel shared].hasLogin) {
            
            MineClassListViewController *controller = [[MineClassListViewController alloc] init];
            [self.tabBarController.navigationController pushViewController:controller animated:YES];
        }else {
            
            UINavigationController *controller = [LoginViewController loginViewController];
            [self.tabBarController.navigationController presentViewController:controller animated:YES completion:nil];
        }
    }else if (indexPath.item == 2) {
        if ([UserModel shared].hasLogin) {
            
            MineFocusViewController *controller = [[MineFocusViewController alloc] init];
            [self.tabBarController.navigationController pushViewController:controller animated:YES];
        }else {
            
            UINavigationController *controller = [LoginViewController loginViewController];
            [self.tabBarController.navigationController presentViewController:controller animated:YES completion:nil];
        }
    }else {
        MineHelpViewController *controller = [[MineHelpViewController alloc] init];
        [self.tabBarController.navigationController pushViewController:controller animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
